package com.apes.scm.masterdata.dcDeliveryArea.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.dcDeliveryArea.model.DcDeliveryArea;
import com.apes.scm.masterdata.dcDeliveryArea.repository.DcDeliveryAreaRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("dcDeliveryAreaService")
public class DcDeliveryAreaService extends DomainService{

    @Autowired
    private DcDeliveryAreaRepository dcDeliveryAreaRepository;

    public DcDeliveryArea save(SimpleRequest request) {
        DcDeliveryArea dcDeliveryArea = request.getO(DcDeliveryArea.class);
        JSONObject creator = JSONObject.parseObject(request.get("creator").toString());
        dcDeliveryArea.setCreator(creator.getString("name"));
        dcDeliveryArea.setCreateDate(new Date());
        return dcDeliveryAreaRepository.saveAndFlush(dcDeliveryArea);
    }

    public DcDeliveryArea update(SimpleRequest request) {
        DcDeliveryArea dcDeliveryArea = request.getO(DcDeliveryArea.class);
        return dcDeliveryAreaRepository.saveAndFlush(dcDeliveryArea);
    }

    public Page<DcDeliveryArea> findAll(SimpleRequest request) {
        List<Map> condition = request.get("condition");
         return dcDeliveryAreaRepository.findAll(JpaDsl.toCriteria(condition), PageRequest.of(request.get("page"), request.get("size")));
    }
}
